import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const profile = ref({})
  const loading = ref(false)

  // 获取用户资料
  const fetchProfile = async () => {
    loading.value = true
    try {
      // 模拟API调用
      // const response = await userAPI.getProfile()
      
      // 模拟数据
      const mockProfile = {
        id: 1,
        name: '张三',
        avatar: '',
        email: 'zhangsan@example.com',
        phone: '13800138000',
        createdAt: '2024-01-01'
      }
      
      profile.value = mockProfile
      return { success: true, data: mockProfile }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  // 更新用户资料
  const updateProfile = async (data) => {
    loading.value = true
    try {
      // 模拟API调用
      // const response = await userAPI.updateProfile(data)
      
      profile.value = { ...profile.value, ...data }
      return { success: true, data: profile.value }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  // 修改密码
  const changePassword = async (passwordData) => {
    loading.value = true
    try {
      // 模拟API调用
      // const response = await userAPI.changePassword(passwordData)
      
      return { success: true, message: '密码修改成功' }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  return {
    profile,
    loading,
    fetchProfile,
    updateProfile,
    changePassword
  }
})